#pragma once
//============================================================================
// Copyright (C) 2003-2013 Brett R. Jones 
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses 
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include <VxUtilLib/VxPktHdr.h>
#include <VxUtilLib/vx_debug.h>
#include <string.h>

#define PKT_LIB_VERSION_BINARY 0x1001

#ifndef ROUND_TO_16BYTE_BOUNDRY
	#define ROUND_TO_16BYTE_BOUNDRY( a ) (( a + 15 ) & ~15 ) //round upto even 16 byte boundry
#endif //ROUND_TO_16BYTE_BOUNDRY

#ifndef MAX_PKT_LEN
	//NOTE: packets greater than 16384 may be blocked by routers/ satellite etc
	#define MAX_PKT_LEN						16384
#endif //MAX_PKT_LEN

//=== application and shell defines ===//
#define MAX_APP_VERSION_STR_LEN			31		// max length of Application Name and version string
#define MAX_SHELL_COMMAND_LEN			255		// maximum length of shell command string
#define IM_ALIVE_TIMEOUT				15		// times in second to close connection if no traffic
#define MAX_SETTING_NAME_LEN			256
#define MAX_SETTING_VALUE_LEN			1024

#define PKT_MAX_URL_LEN					256

//=== login defines ===//
#define PKT_MIN_LOGIN_NAME_LEN			4		// minimum length of login name required
#define PKT_MAX_LOGIN_NAME_LEN			23		// maximum length of login name
#define PKT_MIN_PWD_LEN					6		// minimum length of password required
#define PKT_MAX_PWD_LEN					23		// maximum length of password

#define MAX_ADMIN_LIST_LEN				4095	// max length of list of admins
#define MAX_USER_LIST_LEN				4095	// max length of list of users
#define MAX_LOGON_MSG_LEN				63		// max length of logon message


// debug/logging defines
#define MAX_LOG_ENTRY_LEN				2035	// max length of a log message
#define MAX_DEBUG_PARAM_LEN				512		// max length of debug param
#define MAX_ARG_LEN						255		// maximum length of command argument



// file transfer defines
#define MAX_FILE_LIST_LEN				4096	// maximum length of list of files
#define MAX_FILE_CHUNK_LEN				14336	// maximum length of chunk of file data

// encryption defines
#define MAX_PUBLIC_KEY_LEN				128		// maximum public key length in bytes
#define MAX_SYM_KEY_LEN					16		// maximum symetric key length in bytes


// error defines 
#define MAX_PKT_ERR_MSG_LEN				255		// maximum length of error message
#define MAX_ERR_MSG_LEN					511		// maximum length of error message


#define ERR_CANNOT_REMOVE_CHAR_IN_USE	1
#define ERR_FILE_IO						2
#define ERR_ALL_ACCT_CHARS_SLOTS_FILLED	3
#define ERR_CHAR_NAME_TAKEN				4


//=== game packet defines ===//
#define PKT_MAX_AVATAR_MODEL_NAME_LEN	15	// min len of char name
#define PKT_MIN_AVATAR_NAME_LEN			3	// min len of char name
#define PKT_MAX_AVATAR_NAME_LEN			23	// max len of char name
#define PKT_MAX_MSG_LEN					128
#define PKT_MAX_BUY_CNT					10
#define PKT_MAX_CONTAINER_CONTENT_CNT	128
#define PKT_MAX_TRADE_CNT				10
#define PKT_MAX_CHAR_NAME_LEN		23 //max len of char name
#define PKT_MAX_ITEM_NAME_LEN			23
#define PKT_MAX_PETS_PER_PLAYER			5 // max pets a character can own
#define PKT_MAX_AVATARS_PER_PLAYER		5 // max player characters a account can have


#define PKT_TYPE_INVALID			0 // packet type 0 is not allowed
#define PKT_TYPE_KEY_REQ			1
#define PKT_TYPE_KEY_REPLY			2
#define PKT_TYPE_FILE_CHUNK			3
#define PKT_TYPE_FILE_CHUNK_ACK		4
#define PKT_TYPE_CMD_BIN_REQ		5
#define PKT_TYPE_CMD_BIN_REPLY		6
#define PKT_TYPE_CMD_1ARG_REQ		7
#define PKT_TYPE_CMD_1ARG_REPLY		8
#define PKT_TYPE_CMD_2ARG_REQ		9
#define PKT_TYPE_CMD_2ARG_REPLY		10
#define PKT_TYPE_CMD_3ARG_REQ		11
#define PKT_TYPE_CMD_3ARG_REPLY		12
#define PKT_TYPE_FILE_SEND_COMPLETE	13
#define PKT_TYPE_FILE_SEND_COMPLETE_ACK	14
#define PKT_TYPE_FILE_GET_COMPLETE	15
#define PKT_TYPE_FILE_GET_COMPLETE_ACK	16
#define PKT_TYPE_AVAIL_DRIVES_REQ	17
#define PKT_TYPE_AVAIL_DRIVES_REPLY	18
#define PKT_TYPE_FILE_LIST_REQ		19
#define PKT_TYPE_FILE_LIST_REPLY	20
#define PKT_TYPE_IM_ALIVE_REQ		21
#define PKT_TYPE_IM_ALIVE_REPLY		22
#define PKT_TYPE_CHALLENGE_REQ		23
#define PKT_TYPE_CHALLENGE_REPLY	24
#define PKT_TYPE_SESSION_REQ		25
#define PKT_TYPE_SESSION_REPLY		26
#define PKT_TYPE_LOG_ENTRY			27
#define PKT_TYPE_DEBUG				28
#define PKT_TYPE_UPDATE				29


#define GAME_PKT_TYPE_BASE					50 // this is where game engin packets start

#define PKT_TYPE_ADD_TO_CONTAINER			(GAME_PKT_TYPE_BASE + 1)
#define PKT_TYPE_ADMIN						(GAME_PKT_TYPE_BASE + 2)
#define PKT_TYPE_ANIM						(GAME_PKT_TYPE_BASE + 3)
#define PKT_TYPE_ATTACK_REQ					(GAME_PKT_TYPE_BASE + 4)
#define PKT_TYPE_ATTACK_REPLY				(GAME_PKT_TYPE_BASE + 5)
#define PKT_TYPE_AVATAR_CREATE_REQ			(GAME_PKT_TYPE_BASE + 6)
#define PKT_TYPE_AVATAR_CREATE_REPLY		(GAME_PKT_TYPE_BASE + 7)
#define PKT_TYPE_AVATAR_DELETE_REQ			(GAME_PKT_TYPE_BASE + 8)
#define PKT_TYPE_AVATAR_DELETE_REPLY		(GAME_PKT_TYPE_BASE + 9)
#define PKT_TYPE_AVATAR_PLAY_REQ			(GAME_PKT_TYPE_BASE + 10)
#define PKT_TYPE_AVATAR_PLAY_REPLY			(GAME_PKT_TYPE_BASE + 11)
#define PKT_TYPE_AVATAR_CHECK_NAME_REQ		(GAME_PKT_TYPE_BASE + 12)
#define PKT_TYPE_AVATAR_CHECK_NAME_REPLY	(GAME_PKT_TYPE_BASE + 13)
#define PKT_TYPE_AVATAR_EXIT_REQ			(GAME_PKT_TYPE_BASE + 14)
#define PKT_TYPE_AVATAR_EXIT_REPLY			(GAME_PKT_TYPE_BASE + 15)
#define PKT_TYPE_ENT_POS_REQ		(GAME_PKT_TYPE_BASE + 16)	// entity position and heading
#define PKT_TYPE_ENT_POS_REPLY		(GAME_PKT_TYPE_BASE + 17)
#define PKT_TYPE_ENT_MOVE_REQ		(GAME_PKT_TYPE_BASE + 18)	// entity movement vector + timing
#define PKT_TYPE_ENT_MOVE_REPLY		(GAME_PKT_TYPE_BASE + 19)
#define PKT_TYPE_BUY				(GAME_PKT_TYPE_BASE + 20)
#define PKT_TYPE_CHAR_INFO			(GAME_PKT_TYPE_BASE + 21)
#define PKT_TYPE_CLICK				(GAME_PKT_TYPE_BASE + 22)
#define PKT_TYPE_CONTAINER_CONTENT	(GAME_PKT_TYPE_BASE + 23)
#define PKT_TYPE_DELETE_OBJ			(GAME_PKT_TYPE_BASE + 24)
#define PKT_TYPE_DISCONNECT			(GAME_PKT_TYPE_BASE + 25)
#define PKT_TYPE_DRAGGING			(GAME_PKT_TYPE_BASE + 26)
#define PKT_TYPE_DRAW_CONTAINER		(GAME_PKT_TYPE_BASE + 27)
#define PKT_TYPE_EFFECT				(GAME_PKT_TYPE_BASE + 28)
#define PKT_TYPE_FIGHT_OCCURRING	(GAME_PKT_TYPE_BASE + 29)
#define PKT_TYPE_ITEM_DROP			(GAME_PKT_TYPE_BASE + 30) 
#define PKT_TYPE_ITEM_PICKUP		(GAME_PKT_TYPE_BASE + 31) 
#define PKT_TYPE_ITEM_STATE			(GAME_PKT_TYPE_BASE + 32) //wear item etc
#define PKT_TYPE_LIGHT				(GAME_PKT_TYPE_BASE + 33)
#define PKT_TYPE_LOGIN_REQ			(GAME_PKT_TYPE_BASE + 34)
#define PKT_TYPE_LOGIN_REPLY		(GAME_PKT_TYPE_BASE + 35)
#define PKT_TYPE_MOBILE_ATTR		(GAME_PKT_TYPE_BASE + 36)
#define PKT_TYPE_PLAYER_STAT_REQ	(GAME_PKT_TYPE_BASE + 37)
#define PKT_TYPE_PLAYER_STAT_REPLY	(GAME_PKT_TYPE_BASE + 38)
#define PKT_TYPE_PUNISH				(GAME_PKT_TYPE_BASE + 39)
#define PKT_TYPE_RENAME				(GAME_PKT_TYPE_BASE + 40)
#define PKT_TYPE_SOUND				(GAME_PKT_TYPE_BASE + 41)
#define PKT_TYPE_STAT_WND			(GAME_PKT_TYPE_BASE + 42)
#define PKT_TYPE_TALK				(GAME_PKT_TYPE_BASE + 43)
#define PKT_TYPE_TARGETING			(GAME_PKT_TYPE_BASE + 44)
#define PKT_TYPE_TIME				(GAME_PKT_TYPE_BASE + 45)
#define PKT_TYPE_TRADE				(GAME_PKT_TYPE_BASE + 46)
#define PKT_TYPE_UPDATE_CHAR		(GAME_PKT_TYPE_BASE + 47)
#define PKT_TYPE_WARN				(GAME_PKT_TYPE_BASE + 48)
#define PKT_TYPE_WEATHER			(GAME_PKT_TYPE_BASE + 49)
#define PKT_TYPE_WEB_BROWSE			(GAME_PKT_TYPE_BASE + 50)
#define PKT_TYPE_WORN_ITEM			(GAME_PKT_TYPE_BASE + 47)
#define PKT_TYPE_PET_LIST			(GAME_PKT_TYPE_BASE + 48)
#define PKT_TYPE_GAME_ACCTS_REQ		(GAME_PKT_TYPE_BASE + 49)
#define PKT_TYPE_GAME_ACCTS_REPLY	(GAME_PKT_TYPE_BASE + 50)
#define PKT_TYPE_ENTER_GAME_REQ		(GAME_PKT_TYPE_BASE + 51)
#define PKT_TYPE_ENTER_GAME_REPLY	(GAME_PKT_TYPE_BASE + 52)
#define PKT_TYPE_SRV_ANN_REQ		(GAME_PKT_TYPE_BASE + 53)	// announce server 
#define PKT_TYPE_SRV_ANN_REPLY		(GAME_PKT_TYPE_BASE + 54)	// announce server 
#define PKT_TYPE_ACCT_CREATE_REQ	(GAME_PKT_TYPE_BASE + 55)	// create account request
#define PKT_TYPE_ACCT_CREATE_REPLY	(GAME_PKT_TYPE_BASE + 56)	// create account reply 
#define PKT_TYPE_GAME_LOGIN_CHALLENGE_REQ	(GAME_PKT_TYPE_BASE + 57)	// login challenge
#define PKT_TYPE_GAME_LOGIN_CHALLENGE_REPLY	(GAME_PKT_TYPE_BASE + 58)	// login challenge
#define PKT_TYPE_CREATURE_REQ			(GAME_PKT_TYPE_BASE + 59)	// creature request
#define PKT_TYPE_CREATURE_REPLY			(GAME_PKT_TYPE_BASE + 60)	// creature reply 
#define PKT_TYPE_CREATURE_STATUS_REQ	(GAME_PKT_TYPE_BASE + 61)	// creature status request
#define PKT_TYPE_CREATURE_STATUS_REPLY	(GAME_PKT_TYPE_BASE + 62)	// creature status reply 
#define PKT_TYPE_ITEM_REQ				(GAME_PKT_TYPE_BASE + 63)	// creature request
#define PKT_TYPE_ITEM_REPLY				(GAME_PKT_TYPE_BASE + 64)	// creature reply 
#define PKT_TYPE_ITEM_STATUS_REQ		(GAME_PKT_TYPE_BASE + 65)	// creature status request
#define PKT_TYPE_ITEM_STATUS_REPLY		(GAME_PKT_TYPE_BASE + 66)	// creature status reply 
#define PKT_TYPE_PLAYER_REQ				(GAME_PKT_TYPE_BASE + 67)	// player request
#define PKT_TYPE_PLAYER_REPLY			(GAME_PKT_TYPE_BASE + 68)	// player reply 
#define PKT_TYPE_PLAYER_STATUS_REQ		(GAME_PKT_TYPE_BASE + 69)	// player status request
#define PKT_TYPE_PLAYER_STATUS_REPLY	(GAME_PKT_TYPE_BASE + 70)	// player status reply 
#define PKT_TYPE_PLACEABLE_REQ			(GAME_PKT_TYPE_BASE + 71)	// placeable request
#define PKT_TYPE_PLACEABLE_REPLY		(GAME_PKT_TYPE_BASE + 72)	// placeable reply 
#define PKT_TYPE_PORTAL_REQ				(GAME_PKT_TYPE_BASE + 73)	// portal request
#define PKT_TYPE_PORTAL_REPLY			(GAME_PKT_TYPE_BASE + 74)	// portal reply 
#define PKT_TYPE_TELEPORT_REQ			(GAME_PKT_TYPE_BASE + 75)	// player requests teleport thru portal request
#define PKT_TYPE_TELEPORT_REPLY			(GAME_PKT_TYPE_BASE + 76)	// player requests teleport thru portal reply 

#define MAX_GAME_PKT_TYPE			(GAME_PKT_TYPE_BASE + 100) //NOTE: if you add or remove a packet type you must change this


#define GUSER_PKT_TYPE_BASE			MAX_GAME_PKT_TYPE // user defined game packet types should start here
#define PKT_TYPE_GUSER_150			(GUSER_PKT_TYPE_BASE)
#define PKT_TYPE_GUSER_151			(GUSER_PKT_TYPE_BASE + 1) 

										// NOTE: 0 not allowed.. max type is 250
#define MAX_TOTAL_PKT_TYPE			250 // largest packet type allowed by tronacom

#define CMD_TYPE_CONNECT			0
#define CMD_TYPE_DISCONNECT			1
#define CMD_TYPE_OS_VER				2
#define CMD_TYPE_APP_VER			3
#define CMD_TYPE_EXECUTE			4
#define CMD_TYPE_OPEN_DOC_OR_URL	5
#define CMD_TYPE_FILE_SEND			6
#define CMD_TYPE_FILE_GET			7
#define CMD_TYPE_FILE_DELETE		8
#define CMD_TYPE_FILE_RENAME		9
#define CMD_TYPE_FILE_CHECKSUM		10
#define CMD_TYPE_LIST_DIRECTORY		11

#define MAX_CMD_TYPE				11 //NOTE: if you add or remove a command type you must change this

#define CMD_OPTION_RESUME_IF_FILE_EXIST		0
#define CMD_OPTION_REPLACE_IF_FILE_EXIST	1
#define CMD_OPTION_FAIL_IF_FILE_EXIST		2


// get command description from command type
const char * DescribeCmd( int iCmdType );
int CmdFromDescription( const char * pDesc );

const char * DescribePkt( int iPktType );


